<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/6/006
 * Time: 15:12
 */

namespace app\Lib\Model\Order;


use app\Lib\Exception\BlException;
use app\Lib\Model\DbModel\OrderImg;
use app\Lib\Model\DbModel\OrderPanoramicUrl;
use app\Lib\Model\User\User;

class UpdatePicFile
{
    private $order;
    private $imgArray;
    private $user;
    private $url;
    private $delImgArray;
    public function __construct(Order $order,User $user)
    {
        $this->order = $order;
        $this->user  = $user;
    }

    public function setImgArray($imgArray){
        $this->imgArray = $imgArray;

    }
    public function setPanoramicUrl($url){
        $this->url = $url;
    }
    public function setDelImgArray($imgArray){
        $this->delImgArray = $imgArray;
    }

    /**
     *
     * @return bool
     * @throws BlException
     */
    public function run(){
        if(!empty($this->imgArray)){
            $this->handelImgArray();
        }
        if(!empty($this->url)){
            $this->handelUrl();
        }
        if(!empty($this->delImgArray)){
            $this->delArray();
        }
        return true;
    }

    /**
     *
     * @throws BlException
     */
    private function handelImgArray(){
        $model =  (new OrderImg());
        $this->imgArray = json_decode($this->imgArray,true);
        foreach ($this->imgArray as $v){
            $data = [
                'order_id'=>$this->order->get_order_id(),
                'img_id'=>$v,
                'create_time'=>time(),
                'create_id'=>$this->user->getUid()
            ];
            $result = $model->insert($data);
            if(!$result){
                throw new BlException('添加失败');
            }
        }
        return true;
    }

    /**
     * @return bool
     * @throws BlException
     */
    private function handelUrl(){
        $url = json_decode($this->url,true);
        $model =  (new OrderPanoramicUrl());
        $is_hand = isset($url['is_hand'])?0:1;
//        throw new BlException($url);
        if($url['id'] == 0){
            $data = [
                'order_id'=>$this->order->get_order_id(),
                $model->_path_url=>$url['url'],
                'create_time'=>time(),
                'create_id'=>$this->user->getUid(),
                $model->_is_hand=>$is_hand
            ];
            $result = $model->insert($data);
            if(!$result){
                throw new BlException('添加失败');
            }
        }else{
            $data = [
                'id'=>$url['id'],
                $model->_path_url=>$url['url'],
                $model->_update_time=>time(),
                $model->_update_id=>$this->user->getUid(),
            ];
            $result = $model->update($data);
            if($result === false){
                throw new BlException('添加失败');
            }
        }
        return true;
    }

    /**
     * @return bool
     * @throws BlException
     */
    private function delArray(){
        $model =  (new OrderImg());
        $result = $model->where([$model->_id=>['in',$this->delImgArray]])->delete();
        if($result === false){
            throw new BlException('删除失败');
        }
        return true;
    }

}